<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 22:08:01
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-02 20:50:29
 * @Descripttion: 鉴权接口
 */
namespace app\api\controller\collection\v1_0;
use think\Controller;
use think\Response;
use think\exception\HttpResponseException;
use think\Request;
class Common extends Controller
{
    protected $UserInfo; //用户信息
    protected $noNeedRight = [];
    protected $responseType = 'json';
    protected $tdb = 'fq_';
    protected $agent_id;
    protected $bus_id;

    public function initialize()
	{
        $agent_id = request()->header('agentId');
        $bus_id = request()->header('busId');
        $token = request()->header('token') ?? request()->param('token');
       
        $this->agent_id = $agent_id;
        $this->bus_id = $bus_id;
        $arr = is_array($this->noNeedRight) ? $this->noNeedRight : explode(',', $this->noNeedRight);
        if (!$arr) {
            return false;
        }else{
            if ($token) {
                $this->UserInfo = (new \app\common\model\User())->getToken($token);
            }
        }
        $arr = array_map('strtolower', $arr);
        // 是否存在
     if ( (in_array(strtolower(request()->action()), $arr) || in_array('*', $arr)) && empty($token)) {
      
            self::errorResult('需要登录!!!','',244);
        }
        
        if (!empty($token) && (in_array(strtolower(request()->action()), $arr) || in_array('*', $arr))) {
			$UserInfo = (new \app\common\model\User())->getToken($token);
         
			if (empty($UserInfo)) {
				return $this->errorResult('登录超时','',244);
			} else {
				$this->UserInfo = $UserInfo;
			}
		}
    }   

    protected function successResult($msg = '成功', $data = [], $code = 1, $type = null, array $header = [])
	{
        $this->result($msg, $data, $code, $type, $header);
	}

	protected function errorResult($msg = '失败', $data = [], $code = 0, $type = null, array $header = [])
	{
        $this->result($msg, $data, $code, $type, $header);
	}
      /**
     * 返回封装后的 API 数据到客户端
     * @access protected
     * @param mixed  $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为0
     * @param string $type   输出类型，支持json/xml/jsonp
     * @param array  $header 发送的 Header 信息
     * @return void
     * @throws HttpResponseException
     */
    protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
    {
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'time' => time(),
            'data' => $data,
        ];
        // 如果未设置类型则自动判断
        $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);

        if (isset($header['statuscode'])) {
            $code = $header['statuscode'];
            unset($header['statuscode']);
        } else {
            //未设置状态码,根据code值判断
            $code = $code >= 1000 || $code < 200 ? 200 : $code;
        }
        $response = Response::create($result, $type, $code)->header($header);
        throw new HttpResponseException($response);
    }
}
